package com.webapp.servlet;

import com.myserver.core.Request;
import com.myserver.core.Response;
import com.myserver.core.Servlet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class TestServlet extends Servlet {
    @Override
    public void doGet(Request request, Response response) throws IOException {
        Integer n = Integer.valueOf(request.getRequetsParam().get("n"));
        URL url = new URL("http://localhost:8888/home.html");
        long start = System.currentTimeMillis();
        for(int i=0;i<n;i++){
            HttpURLConnection http = (HttpURLConnection)url.openConnection();
            BufferedReader br = new BufferedReader(new InputStreamReader(http.getInputStream()));
        }
        long end = System.currentTimeMillis();
        response.println("<html><body><center><h2>")
                .println("<h1>测试结果</h1>")
                .println("<html><body><center><h2>")
                .println("请求次数: "+n+"<br>")
                .println("总耗时: "+(end-start)+" ms<br>")
                .println("平均响应时间: "+(end-start)/n+" ms<br><br>")
                .println("</center><h2></body>><br></html> ");
    }

    @Override
    public void doPost(Request request, Response response) {

    }
}
